<?php
// +----------------------------------------------------------------------
// | ManniuRead
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2016 http://manniuyun.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: heitao <read@manniuyun.com>
// +----------------------------------------------------------------------

namespace app\admin\model;

use manniu\model\Base;

use think\Cache;
use think\Request;
use think\Session;

class Admin extends Base
{
    // 只读字段
    protected $readonly = ['username', 'salt', 'regip'];

    // 角色组名称获取器
    protected function getRoleIdAttr($value)
    {
        $role_name = '-';
        $role = AdminRole::getRole($value);
        if($role){
            $role_name = $role->getData('name');
        }
        return $role_name;
    }

    // 管理员密码修改器
    protected function setPasswordAttr($value, $data)
    {
        return md5(md5($value) . $data['salt'] . md5($value));
    }

    /**
     * 管理员验证登录
     *
     * @param  array $admin 管理员信息
     * @param  string $p 明文密码
     * @return int
     * int  aid     登录成功
     * int  -1      用户不存在
     * int  -2      密码不正确
     * int  -3      此管理员无角色组
     * int  -4      此管理员所有角色组被禁用
     */
    public function admin_login($admin, $p)
    {
        // 获取原始数据
        $admin_info = $admin->getData();
        $p = trim($p);
        // 管理员信息验证
        if (!$admin_info) { //管理员不存在
            return -1;
        } elseif (md5(md5($p) . $admin_info['salt'] . md5($p)) != $admin_info['password']) { //密码错误
            return -2;
        } elseif (empty($this->role)) { // 管理员角色组信息验证
            return -3;
        } elseif (!$this->role->status) { // 获取当前管理员的最高权限角色组
            return -4;
        }
        // 保存会话
        Session::set('aid', $admin_info['aid']);
        Session::set('admin', $admin->getData());
        return $admin_info['aid'];
    }

    /**
     * 注册
     */
    public function register($data)
    {
        // 正常注册时，管理员初始化信息
        $data['email'] = strtolower($data['email']);
        $data['username'] = trim($data['username']);
        $data['password'] = trim($data['password']);
        $data['salt'] = substr(md5(rand(0, 999)), 0, 10);
        $data['name'] = trim($data['name']);
        $data['phone'] = trim($data['phone']);
        $data['avatar'] = '';
        $data['role_id'] = $data['role_id'] ? (int)$data['role_id'] : 2;
        $data['regip'] = Request::instance()->ip();
        $data['regtime'] = time();
        //验证保存管理员信息
        $result = $this->allowField(true)->validate('Admin.add')->save($data);
        if (false === $result) {
            // 验证失败 输出错误信息
            return false;
        } else {
            return $this->getData('aid');
        }
    }
}